.study <- "study1"

wrangle_data <- function(.study) {
  file <- str_c("data/", .study, "_survey.csv")

  print(file)
  # Load data ---------------------------------------------------------------

  data <- read_Qualtrics(file) %>%
    filter(complete == 1) %>%
    suppressWarnings()

  # Reshape data for conjoint analysis --------------------------------------

  cj_data <- reshape_conjoint(data, "name") %>%
    left_join(reshape_conjoint(data, "experience"), by = join_by(ResponseId, task, profile)) %>%
    left_join(reshape_conjoint(data, "education"), by = join_by(ResponseId, task, profile)) %>%
    left_join(reshape_conjoint(data, "screening"), by = join_by(ResponseId, task, profile)) %>%
    na.omit()

  # Wrangle data for profile-level analysis ---------------------------------

  cj_response1 <- data %>%
    select(ResponseId, Q11.2:Q17.2) %>%
    pivot_longer(cols = 2:ncol(.), names_to = "task", values_to = "selected") %>%
    mutate(
      task = str_extract(task, "\\d.") %>% as.numeric() - 10,
      selected = str_extract(selected, "\\d") %>% as.numeric()
    )

  cj_response2 <- data %>%
    select(ResponseId, Q20.2:Q25.2) %>%
    pivot_longer(cols = 2:ncol(.), names_to = "task", values_to = "selected") %>%
    mutate(
      task = str_extract(task, "\\d.") %>% as.numeric() - 18,
      selected = str_extract(selected, "\\d") %>% as.numeric()
    )

  cj_data1 <- cj_data %>%
    left_join(cj_response1, by = join_by(ResponseId, task)) %>%
    mutate(selected = ifelse(profile == selected, 1, 0)) %>%
    filter(!is.na(selected))

  cj_data2 <- cj_data %>%
    left_join(cj_response2, by = join_by(ResponseId, task)) %>%
    mutate(selected = ifelse(profile == selected, 1, 0)) %>%
    filter(!is.na(selected))

  profiles <- bind_rows(
    cj_data1 %>% mutate(experiment = "Private company"),
    cj_data2 %>% mutate(experiment = "Public school")
  ) %>%
    mutate(
      Screening = case_when(
        screening == "特に優秀" ~ "Excellent",
        screening == "優秀" ~ "Good",
        screening == "良好" ~ "Fair"
      ),
      Gender = case_when(
        name %in% c("前田颯太", "斎藤拓也", "田中海斗", "金田翔太", "金智勳") ~ "Male",
        name %in% c("山口明日香", "竹内里奈", "青木遥", "張本美咲", "朴恩智") ~ "Female"
      ),
      Name = case_when(
        name %in% c("前田颯太", "斎藤拓也", "田中海斗", "山口明日香", "竹内里奈", "青木遥") ~ "Japanese for Japanese",
        name %in% c("金田翔太", "張本美咲") ~ "Japanese for Zainichi",
        name %in% c("金智勳", "朴恩智") ~ "Korean for Zainichi"
      ),
      Education = case_when(
        education == "千葉大学" ~ "Chiba",
        education == "同志社大学" ~ "Doshisha",
        education == "日本大学" ~ "Nihon",
        education == "早稲田大学" ~ "Waseda",
        education == "東京大学" ~ "Tokyo"
      ),
      Experience = case_when(
        experience == "3年" ~ "3 years",
        experience == "5年" ~ "5 years",
        experience == "7年" ~ "7 years"
      )
    )

  # Wrangle data for profile-pair-level analysis ----------------------------

  profile_pairs <- profiles %>%
    select(ResponseId, experiment, task, profile, selected, Name, Gender) %>%
    pivot_wider(
      names_from = profile,
      values_from = c(selected, Name, Gender),
      id_cols = c(ResponseId, experiment, task)
    ) %>%
    mutate(
      pair = case_when(
        Name_1 == "Japanese for Japanese" & Name_2 == "Japanese for Zainichi" ~ 1,
        Name_1 == "Japanese for Zainichi" & Name_2 == "Japanese for Japanese" ~ 1,
        Name_1 == "Japanese for Japanese" & Name_2 == "Korean for Zainichi" ~ 2,
        Name_1 == "Korean for Zainichi" & Name_2 == "Japanese for Japanese" ~ 2,
        TRUE ~ 0
      ),
      selected = case_when(
        Name_1 == "Japanese for Japanese" & selected_1 == 1 ~ 1,
        Name_2 == "Japanese for Japanese" & selected_2 == 1 ~ 1,
        TRUE ~ 0
      ),
      genders = case_when(
        Gender_1 == "Male" & Gender_2 == "Male" ~ "Male vs. Male",
        Gender_1 == "Male" & Gender_2 == "Female" ~ "Male vs. Female",
        Gender_1 == "Female" & Gender_2 == "Male" ~ "Female vs. Male",
        Gender_1 == "Female" & Gender_2 == "Female" ~ "Female vs. Female"
      ),
      genders = factor(genders,
        levels = c("Male vs. Male", "Female vs. Female", "Male vs. Female", "Female vs. Male")
      )
    ) %>%
    filter(pair != 0)

  # Save data ---------------------------------------------------------------

  out <- list(profiles, profile_pairs, data)

  write_rds(out, str_c("output/", .study, "/data_for_analysis.rds"))
}
